package com.example.demo03.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


import java.util.Collections;


/**
 * SwaggerConfig class is used to configure Swagger for the Spring Boot application.
 * Swagger is a tool that helps in documenting RESTful APIs, making it easier
 * for developers to understand and test the APIs.
 * It generates interactive API documentation that can be accessed through a web UI.
 *
 * This class is annotated with @Configuration, indicating that it is a Spring configuration class,
 * and @EnableSwagger2, which enables Swagger support.
 * 
 * http://localhost:8080/swagger-ui/
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {


    /**
     * Creates and configures a Docket bean, which is the primary interface for Swagger configuration.
     * It is used to define the documentation of the API, including which controllers and paths
     * should be included in the documentation.
     *
     * @return Docket instance configured with the desired properties.
     */
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.SWAGGER_2)
             // Selects the controllers to be included in the documentation.
             // Here, RequestHandlerSelectors.basePackage("com.example.demo03")
             // restricts Swagger to scan only the controllers in the specified package.
            .select()
            .apis(RequestHandlerSelectors.basePackage("com.example.demo03.web"))
             // Selects the paths to be included in the documentation.
             // PathSelectors.any() includes all paths.
            .paths(PathSelectors.any())
            .build()
             // Adds additional information about the API.
            .apiInfo(apiInfo());
    }


    /**
     * Provides detailed information about the API, such as title, description, contact details,
     * license, and terms of service.
     * This information will be displayed in the Swagger UI.
     *
     * @return ApiInfo object containing information about the API.
     */
    private ApiInfo apiInfo() {
        return new ApiInfo(
              // The title of the API.
              "My REST API",
              // A description of the API.
              "Some custom description of API.",
              // The version of the API.
              "API TOS",
              // The terms of service URL.
              "Terms of service",
              // Contact information for the API maintainer.
              new Contact("Contact Name", "www.example.com", "myeaddress@company.com"),
              // The license of the API.
              "License of API",
              // The URL of the license.
              "API license URL",
              // Vendor extensions (not used here).
              Collections.emptyList());
    }
}